//
//  MVCViewController.m
//  Advance_MVC
//
//  Created by Willian on 2021/5/2.
//

#import "MVCViewController.h"

#import "Present.h"
#import "MVCDataSource.h"
#import "MVCMainView.h"
#import "TestTableViewCell.h"

@interface MVCViewController ()

@property (nonatomic, strong) Present *present;
@property (nonatomic, strong) MVCDataSource *mvcDS;

@end

@implementation MVCViewController

- (void)loadView{
    //自定义控制器的View
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    //重新设置视图
    self.view = [[MVCMainView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height)];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    __weak MVCViewController *weakSelf = self;
    
    //初始化 数据源Present，模拟接口请求，返回
    self.present = [[Present alloc] initWithMainView:(MVCMainView *)self.view withDataLoadSuccessBlock:^(NSMutableArray * _Nonnull resArr) {
        __strong  MVCViewController *strongSelf = weakSelf;
        //设置数据 model给到view，具体刷新界面还是由Present来操作
        [strongSelf.mvcDS obtainDataArray:resArr];
    }];
    
    //封装tableView的数据，跟响应回调的代理。
    self.mvcDS = [[MVCDataSource alloc] initWithActionTarget:self.present withCellReuseId:@"CellReuseId" withConfigCellBlock:^(TestTableViewCell * _Nonnull cell, UserModel * _Nonnull userModel, NSIndexPath * _Nonnull indexPath) {
        __strong  MVCViewController *strongSelf = weakSelf;
        //cell回调需要的数据
        cell.indexPath = indexPath;
        //cell的事件响应代理
        cell.delegate = strongSelf.present;
        //设置数据，刷新界面
        [cell refreshUIWithUserModel:userModel];
    }];
    
    //给界面的tableView设置数据源,跟响应代理
    [((MVCMainView *)self.view) setTableViewDataSource:self.mvcDS withActionTarget:self.present];
    
}


@end
